#include "ZPopupLayout.h"
#include "ZBuilder.h"

namespace ZuiLib
{

ZPopupLayout::ZPopupLayout(void)
{
    SetBkColor(0x88888888);
}


ZPopupLayout::~ZPopupLayout(void)
{

}

void ZPopupLayout::SetPos(RECT rc)
{
    ZControl::SetPos(rc);
    if (m_items.IsEmpty()) 
        return;

    InsetRect(&rc, &m_rcInset);
    
    for (int it = 0; it < m_items.GetSize(); it++) {
        ZControl* pControl = static_cast<ZControl*>(m_items[it]);
        if (!pControl->IsVisible()) continue;
        if (pControl->IsFloat()) {
            SetFloatPos(it);
        }
        else {
            RECT rcPadding = pControl->GetPadding();
            SIZE sizeAvailable = MakeSize(rc.right - rc.left - rcPadding.left - rcPadding.right,
                rc.bottom - rc.top - rcPadding.top - rcPadding.bottom);
            SIZE size = pControl->EstimateSize(sizeAvailable);

            RECT rcCtl;
            rcCtl.left = rc.left + (rc.right - rc.left - size.cx) / 2;
            rcCtl.right = rcCtl.left + size.cx;
            rcCtl.top = rc.top + (rc.bottom - rc.top - size.cy) / 2;
            rcCtl.bottom = rcCtl.top + size.cy;
            pControl->SetPos(rcCtl);
        }
    }
}


}//namespace
